﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common.CommandTrees.ExpressionBuilder;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using INSTA.GestionEcole.DAL;

namespace INSTA.EcoleGestion.FORMS
{
    public partial class ProfilProfesseur : System.Web.UI.Page
    { 

        
        protected void Page_Load(object sender, EventArgs e)
        { 
            
            //string myUserName = (string) Session["UserName"];
            //LabelNom.Text = myUserName;
            
            //On récupere L'id tapé dans la page d'accueil
            string email = (string) Session["Email"];
            using (var context = new GestionInstaEntities())
            {

                
                
                {
                    var query = from p in context.Professeur
                                where p.Email == email
                                select p;

                    foreach (var prof in query)
                    {

                        //  Session["myphoto"]= etu.photo;
                        //  LabelPhoto.Text = etu.Photo;
                        LabelNom.Text = prof.Nom;
                        LabelPrenom.Text = prof.Prenom;
                        LabelEmail.Text = prof.Email;
                        LabelTel.Text = prof.Tel;
                        LabelAdresse.Text = prof.Adresse;
                        LabelPromo.Text = prof.Promo;

                        string chemin = @"~\Images\belmahi.jpg"; //+ prof.Nom +
                        // on récupère l'adresse de l'image qu'on affecte à la varaiable chemin (string) 
                        Image1.ImageUrl = chemin;
                    }
                    
                  
                    



                      
                      
                    }
            }
            
        }
        
       

       //On suppose que le professeur a une liste d'étudiant avaec le nom le prenom et l'ID des étudiants
        //Quand le professeur fait l'appel si l'étudiant est absent il le note comme absent avcec la date

       
      
       //Lorsque le prof clique sur ce bouton il est envoyé dans la page note
        // Pour qu'il puisse rajouter des notes a c'est éleves.
        protected void ButtonNoteEleve(object sender, EventArgs e)
        {
            //On récupere La PROMO du prof  pour qu'il envoye les absence de ses étudiants
            Session["PromoProfesseur"] = LabelPromo.Text;
            Response.Redirect("~/Note.aspx");
        }

       //Cette méthode sert a ajouter un cours envoyer par le prof aux étudiants de la 
        //promo concerné
        protected void ButtonCoursPromo110(object sender, EventArgs e)
        {
            Response.Redirect("~/Cours.aspx");
               

            }

        protected void ButtonAbsence(object sender, EventArgs e)
        {
            //On récupere La PROMO du prof pour qu'il donne les notes a ses étudiants.
            Session["PromoProfesseur"] = LabelPromo.Text;
            Response.Redirect("~/Absence.aspx");
        }


        

        
        protected void button(object sender, EventArgs e)
        {
           // bool atLeastOneRowDeleted = false;
          /*  string Date = DateTime.Today.ToString();
            string dat = Date.Substring(0, 10);
            string heure = DateTime.Now.Hour.ToString();
            string minute = DateTime.Now.Minute.ToString();
            string AbsenceMatinOuAprem = null;
                // Access the CheckBox
            foreach (GridViewRow row in GridView1.Rows)
            {
                using (var context = new GestionInstaEntities())
                {
                    // Iterate through the Products.Rows property
                    Absent abs = new Absent();

                    CheckBox cb = (CheckBox) row.FindControl("CheckBoxAbsence");
                    string id = null;
                    if (cb != null && cb.Checked)
                    {


                        string nom = GridView1.Rows[row.RowIndex].Cells[1].Text;
                        string prenom = GridView1.Rows[row.RowIndex].Cells[2].Text;
                        //string dateNaiss = GridView1.Rows[row.RowIndex].Cells[3].Text;
                        string email = GridView1.Rows[row.RowIndex].Cells[4].Text;
                        string promo = GridView1.Rows[row.RowIndex].Cells[5].Text;

                        var query = from etu in context.Etudiant
                            where (etu.Nom == nom) & (etu.Prenom == prenom)
                                  & (etu.email == email) & (etu.Promo == promo)
                                  //& (etu.DateDeNaissance == dateNaiss)
                            select etu;




                        foreach (var etudian in query)
                        {
                            id = Convert.ToString(etudian.ID);
                        }

                        if (Convert.ToInt32(heure) < 12)
                        {
                            AbsenceMatinOuAprem = "AM";
                        }
                        else
                        {
                            AbsenceMatinOuAprem = "PM";
                        }

                        //Requete permettant de savoir si l'absence est déja répertorié donc déja checké.

                       var quer = (from a in context.Absent
                            where (a.Nom == nom) & (a.Prenom == prenom)
                                  & (a.email == email) & (a.Promo == promo)
                                  & (a.DateAbsence.Substring(0, 2) == AbsenceMatinOuAprem)
                            select a).Count();
                        if (quer == 0)
                        {

                            abs.Nom = nom;
                            abs.Prenom = prenom;
                            abs.email = email;
                            abs.DateAbsence = AbsenceMatinOuAprem + ":   " + dat + "" + heure + "h" + minute + "min";
                            abs.idétudiant = id;
                            abs.Promo = promo;
                            context.Absent.Add(abs);
                            context.SaveChanges();
                           
                        }

                    }


                    else
                    {
                        string nom = GridView1.Rows[row.RowIndex].Cells[1].Text;
                        string prenom = GridView1.Rows[row.RowIndex].Cells[2].Text;
                      //  string dateNaiss = GridView1.Rows[row.RowIndex].Cells[3].Text;
                        string email = GridView1.Rows[row.RowIndex].Cells[4].Text;
                        string promo = GridView1.Rows[row.RowIndex].Cells[5].Text;
                        string MatinOUaprem = "";
                        var query = from etu in context.Etudiant
                            where (etu.Nom == nom) & (etu.Prenom == prenom)
                                  & (etu.email == email) & (etu.Promo == promo)
                            select etu;
                        foreach (var etudiant in query)
                        {
                            id = Convert.ToString(etudiant.ID);

                        }
                        var query2 = from ab in context.Absent
                            where ab.idétudiant == id
                            select ab;

                        if (Convert.ToInt32(heure) < 12)
                        {
                            AbsenceMatinOuAprem = "AM";
                        }
                        else
                        {
                            AbsenceMatinOuAprem = "PM";
                        }

                        string recupMatinOuAprem = null;
                        
                        foreach (var absence in query2)
                        {
                            recupMatinOuAprem = absence.DateAbsence.Substring(0, 2);
                            

                            if (recupMatinOuAprem == AbsenceMatinOuAprem)
                            {
                                context.Absent.Remove(absence);
                            }
                        }
                        context.SaveChanges();
                    }

                }

                        
                        //ListBox1.Items.Add(abs.ToString());
                    }
                }

        protected void test(object sender, EventArgs e)
        {
             //Read the file and convert it to Byte Array
    string filePath = FileUpload1.PostedFile.FileName;  
    string filename = Path.GetFileName(filePath);
    string ext = Path.GetExtension(filename);
    string contenttype = String.Empty;
 
    //Set the contenttype based on File Extension
    switch(ext)
    {
        case ".doc":
            contenttype = "application/vnd.ms-word";
            break;
        case ".docx":
            contenttype = "application/vnd.ms-word";
            break;
        case ".xls":
            contenttype = "application/vnd.ms-excel";
            break;
        case ".xlsx":
            contenttype = "application/vnd.ms-excel";
            break;
        case ".jpg":
            contenttype = "image/jpg";
            break;
        case ".png":
            contenttype = "image/png";
            break;
        case ".gif":
            contenttype = "image/gif";
            break;
        case ".pdf":
            contenttype = "application/pdf";
            break;
    }
        */}

      

        
            }
        }

        

      

       
        

       
       
            
            
               

            
        
    



        
    
